import axios from 'axios'
// import { MessageBox, Message } from 'element-ui'
// import store from '@/store'
import qs from 'qs'
import { getToken } from '@/utils/auth'

// create an axios instance
const baseUrl = process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API : 'http://47.97.156.168'

const service = axios.create({
  baseURL: baseUrl, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // 添加全局的loading...
    // ////console.log(config.data instanceof Object)
    config.headers.agent = 'Web'
    if (config.method === 'post') {
      config.headers['content-type'] = 'application/x-www-form-urlencoded'
      config.data = qs.stringify(config.data)
    }
    if (getToken()) {
      config.headers.token = getToken()
      // this.queue[url] = true
    }
    return config
  }, error => {
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  res => {
    const { data, status } = res
    return { data, status }
  }, error => {
    return Promise.reject(error)
  }
)

export default service
